<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!-- Copyright 1997 The Open Group, All Rights Reserved -->
<title>fork</title>
</head><body bgcolor=white>
<center>
<font size=2>
The Single UNIX &reg; Specification, Version 2<br>
Copyright &copy; 1997 The Open Group

</font></center><hr size=2 noshade>
<h4><a name = "tag_000_005_167">&nbsp;</a>NAME</h4><blockquote>
fork - create a new process
</blockquote><h4><a name = "tag_000_005_168">&nbsp;</a>SYNOPSIS</h4><blockquote>
<pre><code>

#include &lt;<a href="systypes.h.html">sys/types.h</a>&gt;
#include &lt;<a href="unistd.h.html">unistd.h</a>&gt;

pid_t fork(void);
</code>
</pre>
</blockquote><h4><a name = "tag_000_005_169">&nbsp;</a>DESCRIPTION</h4><blockquote>
The
<i>fork()</i>
function creates a new process.  The new process (child process) is an exact
copy of the calling process (parent process) except as detailed below.
<ul>
<p>
<li>
The child process has a unique process ID.
<p>
<li>
The child process ID also does not match any active process group ID.
<p>
<li>
The child process has a different parent process ID
(that is, the process ID of the parent process).
<p>
<li>
The child process has its own copy of the parent's file descriptors.
Each of the child's file descriptors refers to the same
open file description with the corresponding file descriptor of the parent.
<p>
<li>
The child process has its own copy of the parent's open directory
streams. Each open directory stream in the child process may share
directory stream positioning with the corresponding directory
stream of the parent.
<p>
<li>
The child process may have its own copy of the parent's message catalogue
descriptors.
<p>
<li>
The child process' values of
<i>tms_utime</i>,
<i>tms_stime</i>,
<i>tms_cutime</i>
and
<i>tms_cstime</i>
are set to 0.
<p>
<li>
The time left until an alarm clock signal is reset to 0.
<p>
<li>
All
<i>semadj</i>
values are cleared.
<p>
<li>
File locks set by the parent process are not inherited by the child process.
<p>
<li>
The set of signals pending for the child process is initialised to
the empty set.
<p>
<li>
Interval timers are reset in the child process.
<p>
<li>
If the Semaphores option is supported,
any semaphores
that are open in the parent process will also be open in the child
process.
<p>
<li>
If the Process Memory Locking option is supported,
the child process does not inherit any address space
memory locks established by the parent process via calls to
<i><a href="mlockall.html">mlockall()</a></i>
or
<i><a href="mlock.html">mlock()</a></i>.
<p>
<li>
Memory mappings created in the parent are retained in the child process.
MAP_PRIVATE mappings inherited from the parent will also be
MAP_PRIVATE mappings in the child,
and any modifications to the data in these mappings
made by the parent prior to calling
<i>fork()</i>
will be visible to the child.
Any modifications to the data in MAP_PRIVATE
mappings made by the parent after
<i>fork()</i>
returns will be visible only to the parent.
Modifications to the data in MAP_PRIVATE
mappings made by the child will be visible only to the child.
<p>
<li>
If the Process Scheduling option is supported,
for the SCHED_FIFO and SCHED_RR scheduling policies,
the child process inherits the policy and priority settings
of the parent process during a
<i>fork()</i>
function.
For other scheduling policies, the policy and priority settings on
<i>fork()</i>
are implementation-dependent.
<p>
<li>
If the Timers option is supported,
per-process timers created by the parent are not inherited
by the child process.
<p>
<li>
If the Message Passing option is supported,
the child process has its own copy
of the message queue descriptors of the parent.
Each of the message descriptors of the child refers
to the same open message queue description
as the corresponding message descriptor of the parent.
<p>
<li>
If the Asynchronous Input and Output option is supported,
no asynchronous input or asynchronous output operations are inherited
by the child process.
<p>
</ul>
<p>
The inheritance of process characteristics not defined by
this document is implementation-dependent.
After
<i>fork()</i>,
both the parent and the child processes are capable of executing
independently before either one terminates.
<p>
A process is created with a single thread.
If a multi-threaded process calls
<i>fork()</i>,
the new process contains a replica of the calling thread
and its entire address space,
possibly including the states of mutexes and other resources.
Consequently, to avoid errors, the child process may only execute
async-signal safe operations until such time as one of the
<i>exec</i>
functions is called.
Fork handlers may be established by means of the
<i><a href="pthread_atfork.html">pthread_atfork()</a></i>
function in order to maintain application invariants across
<i>fork()</i>
calls.
</blockquote><h4><a name = "tag_000_005_170">&nbsp;</a>RETURN VALUE</h4><blockquote>
Upon successful completion,
<i>fork()</i>
returns 0 to the child process and returns the process ID
of the child process to the parent process.
Otherwise, -1 is returned to the parent process, no child process is
created, and
<i>errno</i>
is set to indicate the error.
<br>
</blockquote><h4><a name = "tag_000_005_171">&nbsp;</a>ERRORS</h4><blockquote>
The
<i>fork()</i>
function will fail if:
<dl compact>

<dt>[EAGAIN]<dd>
The system lacked the necessary resources to create another
process, or the system-imposed limit
on the total number of processes under
execution system-wide or by a single user {CHILD_MAX} would be exceeded.

</dl>
<p>
The
<i>fork()</i>
function may fail if:
<dl compact>

<dt>[ENOMEM]<dd>
Insufficient storage space is available.

</dl>
</blockquote><h4><a name = "tag_000_005_172">&nbsp;</a>EXAMPLES</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_005_173">&nbsp;</a>APPLICATION USAGE</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_005_174">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_005_175">&nbsp;</a>SEE ALSO</h4><blockquote>
<i><a href="alarm.html">alarm()</a></i>,
<i><a href="exec.html">exec</a></i>,
<i><a href="fcntl.html">fcntl()</a></i>,
<i><a href="semop.html">semop()</a></i>,
<i><a href="signal.html">signal()</a></i>,
<i><a href="times.html">times()</a></i>,
<i><a href="systypes.h.html">&lt;sys/types.h&gt;</a></i>,
<i><a href="unistd.h.html">&lt;unistd.h&gt;</a></i>.
</blockquote><h4>DERIVATION</h4><blockquote>
Derived from Issue 1 of the SVID.
</blockquote><hr size=2 noshade>
<center><font size=2>
UNIX &reg; is a registered Trademark of The Open Group.<br>
Copyright &copy; 1997 The Open Group
<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]

</font></center><hr size=2 noshade>
</body></html>

